data_5 <- read.csv("C:/Users/cparr/OneDrive - Singapore Management University/Documents/imputed_data_5.csv")
data_4<- read.csv("C:/Users/cparr/OneDrive - Singapore Management University/Documents/imputed_data_4.csv")

library (tidyverse)
library (ggplot2)
library(jtools)
library (officer)
library (flextable)
library (broom.mixed)
library (car)
library(dplyr)
library (ggplot2)
library (ggeffects)
library(cowplot)
library (psych)
library(readr)
library(lmtest)
library(sandwich)
library (psych)


data_5$political_trust_index <- rowSums(data_5[, c("executive", "courts", "national", "parties", "parliament", "local")], na.rm = TRUE)

data_5$protective_trust_index <- rowSums(data_5[, c("military", "police")], na.rm = TRUE)

political_items <- data_5[, c("executive", "courts", "national", "parties", "parliament", "local")]
alpha(political_items)

protective_items <- data_5[, c("military", "police")]
alpha(protective_items)

# For social_media
data_5$social_media_binary <- ifelse(data_5$social_media == 1, 1, 0)

# For express
data_5$express_binary <- ifelse(data_5$express == 1, 1, 0)

df_clean <- na.omit(data_5)


df_clean$social_media_binary <- as.factor(df_clean$social_media_binary)
df_clean$express_binary <- as.factor(df_clean$express_binary)
df_clean$gender <- as.factor(df_clean$gender)
df_clean$employed <- as.factor(df_clean$employed)
df_clean$education <- as.factor(df_clean$education)
df_clean$urban <- as.factor(df_clean$urban)
df_clean$country_name <- as.factor(df_clean$country_name)
df_clean$wave <- as.factor(df_clean$wave)

# Political Trust Model
model_political <- lm(political_trust_index ~ social_media_binary + express_binary + age + gender +
                        employed + education + income + urban + country_name,
                      data = df_clean)

# Protective Trust Model
model_protective <- lm(protective_trust_index ~ social_media_binary + express_binary + age + gender +
                         employed + education + income + urban + country_name,
                       data = df_clean)

summary (model_political)
summary (model_protective)

library(modelsummary)


modelsummary(
  list("Political Trust" = model_political, "Protective Trust" = model_protective),
  statistic = "conf.int",
  stars = TRUE,
  gof_omit = "AIC|BIC|Log.Lik",
  output = "trust_models_wave5.docx"
)

library(ordinal)


df_clean$ngos <- factor(df_clean$ngos, levels = c(1, 2, 3, 4,5 ,6), ordered = TRUE)


model_ngos <- clm(ngos ~ social_media_binary + express_binary + age + gender +
                    employed + education + income + urban + country_name,
                  data = df_clean)

summary (model_ngos)


modelsummary(
  model_ngos,
  statistic = "conf.int",
  stars = TRUE,
  gof_omit = "AIC|BIC|Log.Lik",
  coef_omit = "Intercept|\\|",  
  output = "trust_ngos_wave5.docx"
)

##### WAVE 4

data_4$political_trust_index <- rowSums(data_4[, c("executive", "courts", "national", "parties", "parliament", "local")], na.rm = TRUE)

data_4$protective_trust_index <- rowSums(data_4[, c("military", "police")], na.rm = TRUE)

political_items <- data_4[, c("executive", "courts", "national", "parties", "parliament", "local")]
alpha(political_items)

protective_items <- data_4[, c("military", "police")]
alpha(protective_items)

# For social_media
data_4$social_media_binary <- ifelse(data_4$social_media == 1, 1, 0)

data_4$express_binary <- ifelse(data_4$express %in% 1:5, 1, 
                                ifelse(data_4$express == 6, 0, NA))

df_clean <- na.omit(data_4)

df_clean$social_media_binary <- as.factor(df_clean$social_media_binary)
df_clean$express_binary <- as.factor(df_clean$express_binary)
df_clean$gender <- as.factor(df_clean$gender)
df_clean$employed <- as.factor(df_clean$employed)
df_clean$education <- as.factor(df_clean$education)
df_clean$urban <- as.factor(df_clean$urban)
df_clean$country_name <- as.factor(df_clean$country_name)
df_clean$wave <- as.factor(df_clean$wave)

# Political Trust Model
model_political <- lm(political_trust_index ~ social_media_binary + express_binary + age + gender +
                        employed + education + income + urban + country_name,
                      data = df_clean)

# Protective Trust Model
model_protective <- lm(protective_trust_index ~ social_media_binary + express_binary + age + gender +
                         employed + education + income + urban + country_name,
                       data = df_clean)

summary (model_political)
summary (model_protective)

library(modelsummary)


modelsummary(
  list("Political Trust" = model_political, "Protective Trust" = model_protective),
  statistic = "conf.int",
  stars = TRUE,
  gof_omit = "AIC|BIC|Log.Lik",
  output = "trust_models_wave4.docx"
)

library(ordinal)


df_clean$ngos <- factor(df_clean$ngos, levels = c(1, 2, 3, 4), ordered = TRUE)


model_ngos <- clm(ngos ~ social_media_binary + express_binary + age + gender +
                    employed + education + income + urban + country_name,
                  data = df_clean)

summary (model_ngos)


modelsummary(
  model_ngos,
  statistic = "conf.int",
  stars = TRUE,
  gof_omit = "AIC|BIC|Log.Lik",
  coef_omit = "Intercept|\\|",  
  output = "trust_ngos_wave4.docx"
)


write.csv(data_4, "data_4_appendix.csv", row.names = FALSE)
write.csv(data_5, "data_5_appendix.csv", row.names = FALSE)


##### WAVE 4 POLITICAL INFO CHECK

data_4_rev <- read.csv("C:/Users/cparr/OneDrive - Singapore Management University/Documents/data_4_appendix_rev.csv")

df_clean <- na.omit(data_4_rev)


df_clean$social_media_binary <- as.factor(df_clean$social_media_binary)
df_clean$express_binary <- as.factor(df_clean$express_binary)
df_clean$gender <- as.factor(df_clean$gender)
df_clean$employed <- as.factor(df_clean$employed)
df_clean$education <- as.factor(df_clean$education)
df_clean$urban <- as.factor(df_clean$urban)
df_clean$country_name <- as.factor(df_clean$country_name)
df_clean$wave <- as.factor(df_clean$wave)


# Political Trust Model
model_political <- lm(political_trust_index_rev ~ social_media_binary + info + age + gender +
                        employed + education + income + urban + country_name,
                      data = df_clean)

# Protective Trust Model
model_protective <- lm(protective_trust_index_rev ~ social_media_binary + info + age + gender +
                         employed + education + income + urban + country_name,
                       data = df_clean)

summary (model_political)
summary (model_protective)

political_clustered <- coeftest(model_political, vcov = vcovCL(model_political, cluster = ~country_name))
protective_clustered <- coeftest(model_protective, vcov = vcovCL(model_protective, cluster = ~country_name))


print(political_clustered)

print(protective_clustered)


library(modelsummary)


modelsummary(
  list("Political Trust" = model_political, "Protective Trust" = model_protective),
  statistic = "conf.int",
  stars = TRUE,
  gof_omit = "AIC|BIC|Log.Lik",
  output = "info_wave4_index.docx"
)

library(ordinal)


df_clean$ngos <- factor(df_clean$ngos, levels = c(1, 2, 3, 4), ordered = TRUE)


model_ngos <- clm(ngos ~ social_media_binary + info + age + gender +
                    employed + education + income + urban + country_name,
                  data = df_clean)

summary (model_ngos)


vcov_ngos <- vcovCL(model_ngos, cluster = df_clean$country_name)
coeftest(model_ngos, vcov = vcov_ngos)


modelsummary(
  model_ngos,
  statistic = "conf.int",
  stars = TRUE,
  gof_omit = "AIC|BIC|Log.Lik",
  coef_omit = "Intercept|\\|",  
  output = "info_wave4_ngos.docx"
)




